package com.ruoyi.factorydata.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.github.pagehelper.PageInfo;
import com.ruoyi.factorydata.domain.vo.EquipmentDataVo;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.factorydata.domain.EquipmentData;
import com.ruoyi.factorydata.service.IEquipmentDataService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 设备信息数据Controller
 *
 * @author ruoyi
 * @date 2025-07-04
 */
@RestController
@RequestMapping("/equipmentdata/equipmentdata")
public class EquipmentDataController extends BaseController {
    @Autowired
    private IEquipmentDataService equipmentDataService;

    /**
     * 查询设备信息数据列表
     */
    @PreAuthorize("@ss.hasPermi('equipmentdata:equipmentdata:list')")
    @GetMapping("/list")
    public TableDataInfo list(EquipmentData equipmentData) {
        startPage();
        List<EquipmentDataVo> list = equipmentDataService.selectEquipmentDataList(equipmentData);
        TableDataInfo tableDataInfo = new TableDataInfo();
        tableDataInfo.setRows(list);
        tableDataInfo.setTotal(new PageInfo(list).getTotal());
        return tableDataInfo;
    }

    /**
     * 导出设备信息数据列表
     */
    @PreAuthorize("@ss.hasPermi('equipmentdata:equipmentdata:export')")
    @Log(title = "设备信息数据", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, EquipmentData equipmentData) {
        List<EquipmentDataVo> list = equipmentDataService.selectEquipmentDataList(equipmentData);
        ExcelUtil<EquipmentDataVo> util = new ExcelUtil<EquipmentDataVo>(EquipmentDataVo.class);
        util.exportExcel(response, list, "设备信息数据数据");
    }

    /**
     * 获取设备信息数据详细信息
     */
    @PreAuthorize("@ss.hasPermi('equipmentdata:equipmentdata:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(equipmentDataService.selectEquipmentDataById(id));
    }

    /**
     * 新增设备信息数据
     */
    @PreAuthorize("@ss.hasPermi('equipmentdata:equipmentdata:add')")
    @Log(title = "设备信息数据", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody EquipmentData equipmentData) {
        boolean exists = equipmentDataService.lambdaQuery().eq(EquipmentData::getEquipmentCode, equipmentData.getEquipmentCode()).count() > 0;
        if (exists) {
            return AjaxResult.warn("设备编码已存在");
        }
        boolean exists1 = equipmentDataService.lambdaQuery().eq(EquipmentData::getSerialNo, equipmentData.getSerialNo()).count() > 0;
        if (exists1) {
            return AjaxResult.warn("设备序列号已存在");
        }
        equipmentData.setCreateBy(getUserId().toString());
        return toAjax(equipmentDataService.insertEquipmentData(equipmentData));
    }

    /**
     * 修改设备信息数据
     */
    @PreAuthorize("@ss.hasPermi('equipmentdata:equipmentdata:edit')")
    @Log(title = "设备信息数据", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody EquipmentData equipmentData) {
        boolean exists = equipmentDataService.lambdaQuery()
                .ne(EquipmentData::getId, equipmentData.getId())
                .eq(EquipmentData::getEquipmentCode, equipmentData.getEquipmentCode())
                .count() > 0;
        if (exists) {
            return error("设备编号已存在");
        }
        boolean exists1 = equipmentDataService.lambdaQuery()
                .ne(EquipmentData::getId, equipmentData.getId())
                .eq(EquipmentData::getSerialNo, equipmentData.getSerialNo())
                .count() > 0;
        if (exists1) {
            return error("设备序列号已存在");
        }
        return toAjax(equipmentDataService.updateEquipmentData(equipmentData));
    }

    /**
     * 删除设备信息数据
     */
    @PreAuthorize("@ss.hasPermi('equipmentdata:equipmentdata:remove')")
    @Log(title = "设备信息数据", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(equipmentDataService.deleteEquipmentDataByIds(ids));
    }
}
